import {
    CHANGE_CURRENT_SONG,
    CHANGE_PLAY_LIST,
    CHANGE_CURRENT_SONG_INDEX,
    CHANGE_SEQUENCE,
    CHANGE_LYRIC_LIST,
    CHANGE_CURRENT_LYRIC_INDEX,
    CHANGE_SIMI_SONGS,
    CHANGE_SIMI_PLAYLIST,

    CHANGE_SONG_DETAIL,
    CHANGE_PLAY_LYRIC_LIST
} from "./constants"
import {
    getSongDetail,
    getLyric,
    getSimiSong,
    getSimiPlaylist
} from "@/services/player"

import { parseLyric } from "@/utils/parse-lyric"

//改变当前播放的歌曲对象
export const changeCurrentSongAction = (currentSong) => {
    return {
        type: CHANGE_CURRENT_SONG,
        currentSong
    }
}
export const changePlayListAction = (playList) => {
    return {
        type: CHANGE_PLAY_LIST,
        playList
    }
}
export const changeCurrentSongIndexAction = (index) => {
    return {
        type: CHANGE_CURRENT_SONG_INDEX,
        index
    }
}
export const changeSequenceAction = (sequence) => {
    return {
        type: CHANGE_SEQUENCE,
        sequence
    }
}
export const changeLyricListActio = (lyricList) => {
    return {
        type: CHANGE_LYRIC_LIST,
        lyricList
    }
}
export const changeCurrentLyricIndexAction = (index) => {
    return {
        type: CHANGE_CURRENT_LYRIC_INDEX,
        index
    }
}

export const changePlayerSongDetailAction = (songDetail) => {
    return {
        type: CHANGE_SONG_DETAIL,
        songDetail
    }
}

const changeSimiSongsAction = (res) => ({
    type: CHANGE_SIMI_SONGS,
    simiSongs: res.songs
})

const changeSimiPlaylistAction = (res) => ({
    type: CHANGE_SIMI_PLAYLIST,
    simiPlaylist: res.playlists
})

const changePlayerLyricList = (lyricList) => {
    return {
        type: CHANGE_PLAY_LYRIC_LIST,
        lyricList
    }
}

//上一首 下一首切换歌曲
export const prevNextChangeSong = (tag) => {
    return (dispatch, getState) => {
        dispatch(changeLyricListActio([]));//先把当前redux中的歌词数组制空

        const sequence = getState().getIn(["player", "sequence"]);
        let currentSongIndex = getState().getIn(["player", "currentSongIndex"]);
        const playList = getState().getIn(["player", "playList"]);

        switch (sequence) {
            case 1:
                //随机
                let random = Math.floor(Math.random() * playList.length);//取[0, playList.length)随机数
                currentSongIndex = random;
                break;
            default:
                //顺序(包括单曲循环)
                currentSongIndex += tag;
                if (currentSongIndex >= playList.length) currentSongIndex = 0;
                if (currentSongIndex < 0) currentSongIndex = playList.length - 1;
        }
        dispatch(changeCurrentSongAction(playList[currentSongIndex]));
        dispatch(changeCurrentSongIndexAction(currentSongIndex));
        //请求歌词
        dispatch(getLyricAction(playList[currentSongIndex].id))
    }
}



//对外暴露的action
export const getSongDetailAction = (ids) => {
    return function (dispatch, getState) {
        dispatch(changeLyricListActio([]));//先把当前redux中的歌词数组制空
        //1、先判断播放列表中是否着首歌曲
        const playList = getState().getIn(["player", "playList"]);
        const songIndex = playList.findIndex(ele => ele.id === ids);

        if (songIndex === -1) {
            //没有找到歌曲
            //2.1请求歌曲数据
            getSongDetail(ids).then(res => {
                const song = res.songs && res.songs[0];
                if (!song) return;

                //将最新请求到的歌曲添加到播放列表中
                const newPlayList = [...playList];
                newPlayList.push(song);
                //更新redux中的值
                dispatch(changePlayListAction(newPlayList));
                dispatch(changeCurrentSongIndexAction(newPlayList.length - 1));
                dispatch(changeCurrentSongAction(song));
                //请求歌词
                dispatch(getLyricAction(ids))


            })

        } else {
            //2.2播放列表中有这个歌曲
            dispatch(changeCurrentSongAction(playList[songIndex]));
            dispatch(changeCurrentSongIndexAction(songIndex));
            //2.3请求歌词
            dispatch(getLyricAction(ids))

        }


    }
}

export const getLyricAction = (id) => {
    return dispatch => {
        getLyric(id).then(res => {
            // console.log(res);
            // console.log(res.lrc);
            if ((res.lrc && res.lrc.lyric)) {
                //有歌词得歌曲
                const lyric = res.lrc.lyric;//字符串
                const lyricList = parseLyric(lyric);//数组 [{},{}]
                dispatch(changeLyricListActio(lyricList))
            } else {
                //没有歌词得歌曲
                dispatch(changeLyricListActio([]))
            }
        })
    }
}


export const getSimiPlaylistAction = () => {
    return (dispatch, getState) => {
        const id = getState().getIn(["player", "currentSong"]).id;
        if (!id) return;

        getSimiPlaylist(id).then(res => {
            // console.log(res)
            dispatch(changeSimiPlaylistAction(res));
        })
    }
}

export const getSimiSongAction = () => {
    return (dispatch, getState) => {
        const id = getState().getIn(["player", "currentSong"]).id;
        if (!id) return;

        getSimiSong(id).then(res => {
            // console.log(res);
            dispatch(changeSimiSongsAction(res));
        })
    }
}

export const getPlayerSongDetailAction = (ids) => {
    return (dispatch, getState) => {
        const playList = getState().getIn(["player", "playList"]);

        if (playList.length !== 0) {
            //播放列表中数据，判断是不是在列表中
            const index = playList.findIndex(ele => ele.id === ids);
            if (index !== -1) {
                dispatch(changePlayerSongDetailAction(playList[index]));
            }
        }
        //发送网络请求
        getSongDetail(ids).then(res => {
            const song = res.songs && res.songs[0];
            if (!song) return;

            dispatch(changePlayerSongDetailAction(song));
            //请求歌词
            dispatch(getPlayerLyricAction(ids))


        })
    }
}

export const getPlayerLyricAction = (id) => {
    return dispatch => {
        getLyric(id).then(res => {
            // console.log(res);
            // console.log(res.lrc);
            if ((res.lrc && res.lrc.lyric)) {
                //有歌词得歌曲
                const lyric = res.lrc.lyric;//字符串
                const lyricList = parseLyric(lyric);//数组 [{},{}]
                dispatch(changePlayerLyricList(lyricList))
            } else {
                //没有歌词得歌曲
                dispatch(changePlayerLyricList([]))
            }
        })
    }
}

